﻿using System;
using System.Data;
using System.Windows.Forms;
using MediaPortal.GUI.Library;
using MediaPortal.Dialogs;
using MediaPortal.Player;
using System.Data.SQLite;

namespace SimTV
{
    class SimTV_DBHelpers
    {
        //Connect To SQLite DB
        public static SQLiteConnection OpenDBConnection(string dbPath)
        {
            string dbConnectionString = "Data Source=" + dbPath + "Version=3;New=False;Compress=True";
            SQLiteConnection dbConnection = new SQLiteConnection("Data Source=C:/ProgramData/Team MediaPortal/MediaPortal/database/TVSeriesDatabase4.db3;Version=3;New=False;Compress=True");
            return dbConnection;
        }

        //Execute An SQL Query
        public static DataTable ExecuteSQLQuery(string dbPath, string dbQuery)
        {
            //Create Data Set To Store Results
            DataSet queryResults = new DataSet();

            //Create A Connection To The Database
            SQLiteConnection dbConnection = OpenDBConnection(dbPath);
            dbConnection.Open();
            
            //Create A New Datbase Adapter        
            SQLiteDataAdapter DB_Adapter = new SQLiteDataAdapter(dbQuery, dbConnection);
            
            //Fill Data Set With Query Results Adn Extract Datatable To Return
            DB_Adapter.Fill(queryResults);
            DataTable retVal = queryResults.Tables[0];

            //Close The Database Connection And Return
            dbConnection.Close();
            return retVal;
        }

    }
}
